package com.atguigu.gulimall.auth.controller;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.auth.feign.MemberServiceFeign;
import com.atguigu.gulimall.auth.service.LoginService;
import com.atguigu.gulimall.auth.vo.LoginVo;
import com.atguigu.gulimall.auth.vo.MemberEntity;
import com.atguigu.gulimall.auth.vo.RegVo;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: jianPing
 * @date: 2022/3/30 17:30
 * @description:
 */
@Controller
public class LoginController {
    @Autowired
    LoginService loginService;

    @Autowired
    MemberServiceFeign memberServiceFeign;

    @PostMapping("/login")
    public String login(@Valid LoginVo loginVo, BindingResult result, HttpSession session, RedirectAttributes redirectAttributes) {
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            redirectAttributes.addAttribute("errors", errors);
            return "redirect:http://auth.gulimall.com/login.html";
        }
        try {
            R r = memberServiceFeign.findByUsername(loginVo.getLoginacct());
            MemberEntity user = r.getData(new TypeReference<MemberEntity>() {
            });
            if (user != null) {
                BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
                boolean matches = passwordEncoder.matches(loginVo.getPassword(), user.getPassword());
                if (matches) {
                    session.setAttribute("loginUser",user);
                    return "redirect:http://gulimall.com";
                }
            }
            Map<String, String> errors = new HashMap<>();
            errors.put("msg", "用户名或密码错误");
            redirectAttributes.addFlashAttribute("errors", errors);
            return "redirect:http://auth.gulimall.com/login.html";
        } catch (Exception e) {
            Map<String, String> errors = new HashMap<>();
            errors.put("msg", "调用远程服务失败");
            redirectAttributes.addFlashAttribute("errors", errors);
            return "redirect:http://auth.gulimall.com/login.html";
        }
    }


    @PostMapping("/register")
    public String register(@Valid RegVo regVo, BindingResult result, RedirectAttributes redirectAttributes) {
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            redirectAttributes.addAttribute("errors", errors);
            return "redirect:http://auth.gulimall.com/reg.html";
        }
        loginService.register(regVo);
        return "redirect:http://auth.gulimall.com/login.html";
    }
}
